//
//  LoginBaseVC.swift
//  whale
//
//  Created by topnet on 2022/3/29.
//

import UIKit

import YYText
class LoginBaseVC: UIViewController {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.isNavigationBarHidden = true
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
        initFrame()
    }
    
    func initUI() {
        view.backgroundColor = .white
        bgIv.addSubview(helloLbl)
        contentV.da.addSubviews(sub: [titleLbl])
        view.addSubview(bgIv)
        view.addSubview(contentV)
        view.addSubview(backBtn)
        
    }
    
    func initFrame() {
        backBtn.frame = CGRect(x: 0, y: UIScreen.statusBarH, width: 40.adapter, height: 40.adapter)
        
        helloLbl.frame = CGRect(x: 30.adapter, y: backBtn.frame.maxY + 15.adapter, width: view.bounds.width-60.adapter, height: 100.adapter)
        bgIv.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: 300.adapter)
        
        contentV.frame = CGRect(x: 0, y: helloLbl.frame.maxY + 15.adapter, width: view.bounds.width, height: view.bounds.height - helloLbl.frame.maxY - 15.adapter)
        contentV.layer.addCorner(30.adapter, roundRect: [.topLeft, .topRight])
        
        titleLbl.left(helloLbl.frame.minX).top(20.adapter).right(contentV.bounds.width-20.adapter).height(30.adapter)
        nextBtn.left_right(titleLbl).height(50.adapter)
        nextBtn.setBackgroundImage(UIImage.createImg(color: ._blue, rect: nextBtn.bounds), for: .normal)
        nextBtn.setBackgroundImage(UIImage.createImg(color: .bg, rect: nextBtn.bounds), for: .disabled)
        
        if agreementV.superview != nil {
            
        }
    }
    
    func setupAgreementText() {
        let text = "登录注册代表同意".text(font: .font(13), color: .content)
        let at1 = "《用户协议》".text(font: .font(13), color: ._blue)
        at1.yy_setTextHighlight(at1.yy_rangeOfAll(), color: ._blue, backgroundColor: nil) { [weak self] _, _, _, _ in
            self?.actionAgreement()
        }
        text.append(at1)
        let at2 = "和".text(font: .font(13), color: .content)
        text.append(at2)
        let at3 = "《隐私政策》".text(font: .font(13), color: ._blue)
        at3.yy_setTextHighlight(at3.yy_rangeOfAll(), color: ._blue, backgroundColor: nil) { [weak self] _, _, _, _ in
            self?.actionPrivacy()
        }
        text.append(at3)
        agreementV.lbl.attributedText = text
        
        
    }
    
    func setupLoginText(_ isLogin: Bool) {
        let text = (isLogin ? "还没有账号？" : "已有账号，").text(font: .font(15), color: .content)
        let actionT = (isLogin ? "去注册" : "去登录").text(font: .font(15), color: ._blue)
        actionT.yy_setTextHighlight(actionT.yy_rangeOfAll(), color: ._blue, backgroundColor: nil) { [weak self] _, _, _, _ in
            self?.actionLoginSwitch(isLogin)
        }
        text.append(actionT)
        text.yy_alignment = .center
        loginLbl.attributedText = text
    }
    
    private func actionAgreement() {
        goAgreement(url: "https://open.tzey.cn/haoyudata/protocol.html")
    }
    
    private func actionPrivacy() {
        goAgreement(url: "https://open.tzey.cn/haoyudata/law.html")
    }
    
    private func goAgreement(url: String) {
        let vc = AgreementVC()
//        vc.load(url)
        push(vc)
    }
    
    private func actionLoginSwitch(_ isLogin: Bool) {
        if let vcs = navigationController?.viewControllers, vcs.count > 1, let vc = vcs[vcs.count-2] as? LoginBaseVC {
            if vc.isKind(of: RegisterVC.classForCoder()) || vc.isKind(of: LoginVC.classForCoder()) {
                navigationController?.popViewController(animated: true)
                return
            }
        }
        if isLogin {
            let vc = RegisterVC()
            navigationController?.pushViewController(vc, animated: true)
        }else {
            let vc = LoginVC()
            navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    func actionNext() {
        view.endEditing(false)
        if !agreementV.btn.isSelected {
            HUD.toast("请勾选下方同意后进行登录")
            agreementV.rock()
            return
        }
    }
    
    var disposeBag = DisposeBag()

    lazy var bgIv: UIImageView = {
        let iv = UIImageView()
        iv.image = UIImage(contentsOfFile: Bundle.main.path(forResource: "login_bg", ofType: ".png")!)
        iv.contentMode = .scaleAspectFill
        return iv
    }()
    
    lazy var contentV: UIView = {
        let v = UIView()
        v.backgroundColor = .white
        return v
    }()
    
    lazy var helloLbl: UILabel = {
        let lbl = UILabel()
        lbl.text = "您好\n欢迎来到" + Bundle.main.name
        lbl.font = .font(30.adapter, weight: .bold)
        lbl.textColor = .white
        lbl.numberOfLines = 2
        return lbl
    }()
    
    lazy var titleLbl: UILabel = {
        let lbl = UILabel()
        lbl.font = .font(18.adapter, weight: .bold)
        lbl.textColor = .title
        return lbl
    }()
    
    lazy var backBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitleColor(.white, for: .normal)
        btn.iconTitle(IconFont.arrow_left_bold, fontSize: 20.adapter, for: .normal)
        btn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 12.adapter, bottom: 0, right: 0)
        btn.contentHorizontalAlignment = .left
        btn.addTarget(self, action: #selector(back), for: .touchUpInside)
        return btn
    }()
    
    lazy var nextBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitleColor(.white, for: .normal)
        btn.setTitleColor(.content, for: .disabled)
        btn.layer.cornerRadius = 25.adapter
        btn.layer.masksToBounds = true
        btn.isEnabled = false
        btn.rx.tap.throttle(.milliseconds(800), scheduler: MainScheduler.instance).subscribe { [weak self] _ in
            self?.actionNext()
        }.disposed(by: disposeBag)
        return btn
    }()

    
    
    lazy var agreementV = LoginAgreementView()
    
    lazy var loginLbl = YYLabel()
    
    
    deinit {
        LogUtil.log("\(classForCoder) 释放了")
    }
    
}

class LoginAgreementView: UIView {
    lazy var lbl = YYLabel()
    lazy var btn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitleColor(.content, for: .normal)
        btn.setTitleColor(._blue, for: .selected)
        btn.iconTitle(IconFont.check_circle_n, fontSize: 16.adapter, for: .normal)
        btn.iconTitle(IconFont.check_circle_y, fontSize: 16.adapter, for: .selected)
        btn.width_height(30.adapter)
        btn.contentHorizontalAlignment = .right
        btn.contentEdgeInsets = UIEdgeInsets(right: 5.adapter)
        btn.addTarget(self, action: #selector(actionIsAgreement), for: .touchUpInside)
        return btn
    }()
    
    required convenience init() {
        self.init(frame: .zero)
        addSubview(lbl)
        addSubview(btn)
    }
    
    @objc private func actionIsAgreement() {
        btn.isSelected = !btn.isSelected
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        lbl.top_bottom_offsetLeft(btn).right(bounds.width)
    }
}
